package cn.clq.ymcc.consumer;

import cn.clq.ymcc.dto.CourseOnSendMessageDto;
import cn.clq.ymcc.service.IMessageSmsService;
import com.alibaba.fastjson.JSONObject;
import org.apache.rocketmq.common.message.MessageExt;
import org.apache.rocketmq.spring.annotation.RocketMQMessageListener;
import org.apache.rocketmq.spring.core.RocketMQListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import java.nio.charset.StandardCharsets;
import static cn.clq.ymcc.constants.MqConstants.*;

@Component
@RocketMQMessageListener(
        consumerGroup="message-consumer-group",
        topic=COURSE_MESSAGE_TOPIC,
        selectorExpression=COURSE_MESSAGE_TAGS
)
public class CourseOnSendMessageConsumer implements RocketMQListener<MessageExt> {

    @Autowired
    private IMessageSmsService messageSmsService;

    @Override
    public void onMessage(MessageExt messageExt) {
        System.out.println("进入消费者");
        byte[] body = messageExt.getBody();
        if(body == null || body.length == 0){
            return;
        }
        String jsonString = new String(body, StandardCharsets.UTF_8);
        CourseOnSendMessageDto messageDto = JSONObject.parseObject(jsonString, CourseOnSendMessageDto.class);
        messageSmsService.sendMessage(messageDto);
    }
}
